package com.zy.config;

import com.zy.service.ToolServices;
import dev.langchain4j.community.model.dashscope.QwenEmbeddingModel;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.*;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: AI_langchain4j
 * @description:
 * @author: zy
 * @create: 2025-07-01 10:44
 */
@Configuration
public class AiConfig {

    //////////高级API:  带记忆，区分用户,支持流式会话
    public interface Assistant{
        @SystemMessage("你是一位电商网站的客服")
        String chat(@MemoryId String memoryId, @UserMessage String question );  // 需要有一个ChatModel
        @SystemMessage("你是一位电商网站的客服")
        TokenStream chatStream( @MemoryId String memoryId, @UserMessage String question );  //需要有一个StreamingChatModel

    }

    //TODO: 以后修改为 clickhouse/ES/redis...
    @Bean
    public InMemoryEmbeddingStore<TextSegment> embeddingStore() {
        return new InMemoryEmbeddingStore<>();
    }

    //生成代理对象，这个代理 对象要被spring托管, ->才能注入到Controller
    //                     IOC
    @Bean
    public Assistant assistant(ChatModel chatModel, StreamingChatModel streamingChatModel, ChatMemoryStore chatMemoryStore, ToolServices tools,
                               EmbeddingStore embeddingStore,
                               QwenEmbeddingModel qwenEmbeddingModel){
       // PersistentChatMemoryStore memoryStore=new PersistentChatMemoryStore();  利用spring 的Di注入 chatMemoryStore对象
        ChatMemoryProvider chatMemoryProvider= memoryId-> MessageWindowChatMemory.builder()
                .id(   memoryId )
                .maxMessages(1000)
                .chatMemoryStore(   chatMemoryStore )
                .build();

        EmbeddingStoreContentRetriever retriever = EmbeddingStoreContentRetriever.builder()
                .embeddingModel(qwenEmbeddingModel)
                .embeddingStore(embeddingStore)
                .build();

        Assistant assistant= AiServices.builder(Assistant.class)
                .chatModel(  chatModel )
                .streamingChatModel( streamingChatModel  )
                .chatMemoryProvider(  chatMemoryProvider )
                .tools(  tools )    //工具
                .contentRetriever(retriever)
                .build();
        return assistant;

    }

//    @Bean
//    public InMemoryEmbeddingStore embeddingStore() {
//        List<Document> documents = FileSystemDocumentLoader.loadDocuments("E:\\testdocuments");
//        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
//        EmbeddingStoreIngestor.ingest(documents, embeddingStore);
//
//       return embeddingStore;
//    }





}
